package cn.iocoder.yudao.module.game.controller.admin.realtimeUserList;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.game.controller.admin.realtimeUserList.vo.*;
import cn.iocoder.yudao.module.game.dal.dataobject.realtimeUserList.RealtimeUserListDO;
import cn.iocoder.yudao.module.game.service.realtimeUserList.RealtimeUserListService;

@Tag(name = "管理后台 - 实时用户列")
@RestController
@RequestMapping("/game/realtime-user-list")
@Validated
public class RealtimeUserListController {

    @Resource
    private RealtimeUserListService realtimeUserListService;

    @PostMapping("/create")
    @Operation(summary = "创建实时用户列")
    @PreAuthorize("@ss.hasPermission('game:realtime-user-list:create')")
    public CommonResult<Long> createRealtimeUserList(@Valid @RequestBody RealtimeUserListSaveReqVO createReqVO) {
        return success(realtimeUserListService.createRealtimeUserList(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新实时用户列")
    @PreAuthorize("@ss.hasPermission('game:realtime-user-list:update')")
    public CommonResult<Boolean> updateRealtimeUserList(@Valid @RequestBody RealtimeUserListSaveReqVO updateReqVO) {
        realtimeUserListService.updateRealtimeUserList(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除实时用户列")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('game:realtime-user-list:delete')")
    public CommonResult<Boolean> deleteRealtimeUserList(@RequestParam("id") Long id) {
        realtimeUserListService.deleteRealtimeUserList(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得实时用户列")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('game:realtime-user-list:query')")
    public CommonResult<RealtimeUserListRespVO> getRealtimeUserList(@RequestParam("id") Long id) {
        RealtimeUserListDO realtimeUserList = realtimeUserListService.getRealtimeUserList(id);
        return success(BeanUtils.toBean(realtimeUserList, RealtimeUserListRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得实时用户列分页")
    @PreAuthorize("@ss.hasPermission('game:realtime-user-list:query')")
    public CommonResult<PageResult<RealtimeUserListRespVO>> getRealtimeUserListPage(@Valid RealtimeUserListPageReqVO pageReqVO) {
        PageResult<RealtimeUserListDO> pageResult = realtimeUserListService.getRealtimeUserListPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, RealtimeUserListRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出实时用户列 Excel")
    @PreAuthorize("@ss.hasPermission('game:realtime-user-list:export')")
    @OperateLog(type = EXPORT)
    public void exportRealtimeUserListExcel(@Valid RealtimeUserListPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<RealtimeUserListDO> list = realtimeUserListService.getRealtimeUserListPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "实时用户列.xls", "数据", RealtimeUserListRespVO.class,
                        BeanUtils.toBean(list, RealtimeUserListRespVO.class));
    }

}